package datastructure;

import java.util.Scanner;

/**
 * @Author leecho
 * @Date: 2021/10/10 10:07
 * @Version 1.0
 */

public class ArrayQueue {
    public static void main(String[] args) {
        int key1;
        boolean loop = true;
        char key = ' ';

        System.out.println("请输入要创建队列的大小：");
        Scanner scanner = new Scanner(System.in);
        key1 = scanner.nextInt();

        ArrQueue queue = new ArrQueue(key1);

        while (loop) {
            System.out.println("==========请输入以下序号==========");
            System.out.println("s(show): 输出队列");
            System.out.println("a(add): 添加队列");
            System.out.println("g(get): 取出队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("=================================");

            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    int n = 0;
                    System.out.println("请输入要添加的数据：");
                    n = scanner.nextInt();
                    queue.addQueue(n);
                    break;
                case 'g':
                    System.out.println(queue.getQueue());
                    break;
                case 'e':
                    loop = false;
                    scanner.close();
                    break;
                default:
                    System.out.println("请输入正确的序号");
                    break;
            }
        }
    }

}

class ArrQueue{
    /*
    * 思路
    * - 创建一个数组设定大小，设定两个变量来指向数组的前和后
    *   - 两个变量的初始值都是-1，这是因为当入队一个数值时，rear往后移动一个
    *   - 出队一个数值时，front往前移动一个起到出队的效果
    * - 这里为了达到队列的循环利用使用取模的方法，并且下面的实现方式会导致队列的数组有一个位置是不能利用的
    * */
    private int[] arr;
    private int maxSize;
    private int front;
    private int rear;

    public ArrQueue(int maxSize){
        this.maxSize = maxSize;
        arr = new int[maxSize];
        this.front = -1;
        this.rear = -1;
    }

    // 判断是否为空
    public boolean isEmpty(){
        return this.front == this.rear;
    }

    public boolean isFull(){
        return (rear + 1)%maxSize == front;
    }

    public void addQueue(int n){
        if (isFull()) {
            System.out.println("队列已满。无法添加");
            return;
        }
        else {
            this.rear++;
            if (rear == maxSize){
                rear = 0;
            } else {
                rear = rear % maxSize;
            }
            arr[rear] = n;
        }
    }

    public int getQueue(){
        if (isEmpty())
            throw new RuntimeException("队列为空，无法取出");
        else {
            this.front++;
            if (front == maxSize){
                front = 0;
            } else {
                front = front % maxSize;
            }
            return this.arr[front];
        }
    }

    public void showQueue(){
        if (isEmpty()) {
            System.out.println("当前队列为空，没有数据");
            return;
        } else {
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
        }
    }
}